package com.hrrt.home.admin.controller.manage;

import com.hrrt.common.base.BaseController;
import com.hrrt.home.common.constant.HomeResult;
import com.hrrt.home.common.constant.HomeResultConstant;
import com.hrrt.home.dao.model.HomeBaseUnit;
import com.hrrt.home.dao.model.HomeBaseUnitExample;
import com.hrrt.home.rpc.api.HomeBaseUnitService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.apache.commons.lang.StringUtils;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.*;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * 家庭基础单位controller
 * Created by JiangyanPeng on 2017/09/01.
 */
@Controller
@RequestMapping("/manage/baseunit")
@Api(value = "家庭基础单位", description = "家庭基础单位")
public class HomeBaseUnitController extends BaseController {

    @Autowired
    HomeBaseUnitService homeBaseUnitService ;
    private static Logger _log = LoggerFactory.getLogger(HomeBaseUnitController.class);
    @ApiOperation(value = "家庭基础单位首页")
    @RequiresPermissions("home:baseunit:read")
    @RequestMapping(value = "/index", method = RequestMethod.GET)
    public String index() {
        return "/manage/baseunit/index.jsp";
    }
    @ApiOperation(value = "家庭基础单位列表")
    @RequestMapping(value="list",method = RequestMethod.GET)
    @RequiresPermissions("home:baseunit:read")
    @ResponseBody
    public Object list (@RequestParam(required = false, defaultValue = "0", value = "offset") int offset,
                        @RequestParam(required = false, defaultValue = "10", value = "limit") int limit,
                        @RequestParam(required = false, value = "sort") String sort,
                        @RequestParam(required = false, value = "order") String order){
        HomeBaseUnitExample homeBaseUnitExample = new HomeBaseUnitExample();
        if (!StringUtils.isBlank(sort) && !StringUtils.isBlank(order)) {
            homeBaseUnitExample.setOrderByClause(sort + " " + order);
        }
        List<HomeBaseUnit> rows = homeBaseUnitService.selectByExampleForOffsetPage(homeBaseUnitExample,offset,limit);
        long total = homeBaseUnitService.countByExample(homeBaseUnitExample);
        Map<String, Object> result = new HashMap<>();
        result.put("rows", rows);
        result.put("total", total);
        return result;
    }

    @ApiOperation(value = "新增家庭基础单位")
    @RequiresPermissions("home:baseunit:create")
    @RequestMapping(value = "/create", method = RequestMethod.GET)
    public String create() {
        return "/manage/baseunit/create.jsp";
    }

    @ApiOperation(value = "新增家庭基础单位")
    @RequiresPermissions("home:baseunit:create")
    @RequestMapping(value = "/create", method = RequestMethod.POST)
    @ResponseBody
    public Object create(HomeBaseUnit homeBaseUnit) {
        int count = homeBaseUnitService.insertSelective(homeBaseUnit);
        return new HomeResult(HomeResultConstant.SUCCESS,count);
    }

    @ApiOperation(value = "修改家庭基础单位")
    @RequiresPermissions("home:baseunit:update")
    @RequestMapping(value = "/update/{id}", method = RequestMethod.GET)
    public String update(@PathVariable("id") int id, ModelMap modelMap) {
        HomeBaseUnit homeBaseUnit = homeBaseUnitService.selectByPrimaryKey(id);
        modelMap.put("homeBaseUnit", homeBaseUnit);
        return "/manage/baseunit/update.jsp";
    }

    @ApiOperation(value = "修改家庭基础单位")
    @RequiresPermissions("home:baseunit:update")
    @RequestMapping(value = "/update/{id}", method = RequestMethod.POST)
    @ResponseBody
    public Object update(@PathVariable("id") int id, HomeBaseUnit homeBaseUnit) {
        homeBaseUnit.setId(id);
        int count = homeBaseUnitService.updateByPrimaryKeySelective(homeBaseUnit);
        return new HomeResult(HomeResultConstant.SUCCESS,count);
    }

    @ApiOperation(value = "删除家庭基础单位")
    @RequiresPermissions("home:baseunit:delete")
    @RequestMapping(value = "/delete/{ids}",method = RequestMethod.GET)
    @ResponseBody
    public Object delete(@PathVariable("ids") String ids) {
        int count = homeBaseUnitService.deleteByPrimaryKeys(ids);
        return new HomeResult(HomeResultConstant.SUCCESS,count);
    }
}
